package com.wxw.BaseLibrary.component;

import android.app.ActionBar;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.wxw.BaseLibrary.drawable.LayoutParamTool;
import com.wxw.BaseLibrary.drawable.PanelStyle;
import com.wxw.BaseLibrary.element.IListBoxItem;

/**
 * Created by david on 2014-07-11.
 */
public class ListBoxEditItem implements IListBoxItem {
    private TextView tv_caption;
    private EditText et_value;
    private ListBox parent;
    private String key;
    private boolean bChanged = false;
    private String caption = "";
    private String value = "";
    private boolean enable = true;
    private int[] BackgroundColor = new int[]{Color.parseColor("#ffffff")};
    private int BorderColor = Color.parseColor("#17a0ff");
    private int BorderWidth = 0;
    private int radians = 0;
    private LinearLayout linearLayout;
    private ListBox.OnListBoxItemChangedListener listBoxItemChangedListener;

    public ListBoxEditItem(ListBox parent) {
        this.parent = parent;
    }

    @Override
    public View getView() {
        if (linearLayout==null) {
            initView();
        }
        return linearLayout;
    }

    public void setCaption(String caption){
        this.caption = caption;
        if (tv_caption != null) {
            tv_caption.setText(caption);
        }
    }

    public String getCaption() {
        return caption;
    }

    public void setValue(String value) {
        this.value = value;
        if (et_value != null) {
            et_value.setText(value);
        }
    }

    public String getValue() {
        value = et_value != null?et_value.getText().toString():value;
        return value;
    }

    @Override
    public void setKey(String key) {
        this.key = key;
    }

    @Override
    public String getKey() {
        return key;
    }

    @Override
    public void setChanged(boolean b) {
        bChanged = b;
        et_value.setBackground(PanelStyle.createPanelStyle(GradientDrawable.Orientation.TOP_BOTTOM,
                new int[]{(bChanged?parent.getItemChangedBgColor():Color.WHITE)}, 0, 0, 0));

    }

    @Override
    public boolean isChanged() {
        return bChanged;
    }

    @Override
    public void setOnListBoxItemChangedListener(ListBox.OnListBoxItemChangedListener listener) {
        listBoxItemChangedListener = listener;
    }

    @Override
    public ListBox getParent() {
        return parent;
    }

    public void setHint(String hint){
        et_value.setHint(hint);
    }

    public void setEnabled(boolean enable){
        this.enable = enable;
        if (et_value!=null) {
            et_value.setEnabled(enable);
        }
    }

    public boolean isEnabled(){
        return enable;
    }

    public int[] getBackgroundColor() {
        return BackgroundColor;
    }

    public void setBackgroundColor(int[] backgroundColor) {
        BackgroundColor = backgroundColor;
        refreshLayoutBackground();
    }

    public int getBorderColor() {
        return BorderColor;
    }

    public void setBorderColor(int borderColor) {
        BorderColor = borderColor;
        refreshLayoutBackground();
    }

    public int getBorderWidth() {
        return BorderWidth;
    }

    public void setBorderWidth(int borderWidth) {
        BorderWidth = borderWidth;
        refreshLayoutBackground();
    }

    public int getRadians() {
        return radians;
    }

    public void setRadians(int radians) {
        this.radians = radians;
        refreshLayoutBackground();
    }

    private void refreshLayoutBackground(){
        if (linearLayout!=null) {
            linearLayout.setBackground(PanelStyle.createPanelStyle(null, BackgroundColor, BorderWidth, BorderColor, radians));
        }
    }

    private void initView(){
        linearLayout = new LinearLayout(parent.getContext());
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setBackground(PanelStyle.createPanelStyle(null, BackgroundColor, BorderWidth, BorderColor, radians));
        linearLayout.setLayoutParams(LayoutParamTool.createParams(5, 10, 5, 10));

        tv_caption = new TextView(parent.getContext());
        tv_caption.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
        tv_caption.setBackgroundColor(Color.WHITE);
        tv_caption.setTextColor(Color.BLACK);
        tv_caption.setText(caption);
        linearLayout.addView(tv_caption, new LinearLayout.LayoutParams(this.parent.getCaptionWidth(), LinearLayout.LayoutParams.MATCH_PARENT));

        et_value = new LineEditText(parent.getContext());
        et_value.setText(value);
        et_value.setEnabled(enable);
        et_value.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                triggerEvent(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
//        editHelperClass.editText.setBackground(PanelStyle.createPanelStyle(GradientDrawable.Orientation.TOP_BOTTOM,
//                new int[]{getResources().getColor(R.color.cc_white),getResources().getColor(R.color.cc_white)}, 0, 0, 0));
//        et_value.setBackgroundColor(Color.parseColor("#ff8a9089"));
        et_value.setBackground(null);
//        et_value.setBackground(PanelStyle.createWhitePanelStyle());
        et_value.setTextColor(Color.BLACK);
        linearLayout.addView(et_value, LayoutParamTool.createParams(5, 10, 5, 10));
    }

    private void triggerEvent(Object value){
        if (listBoxItemChangedListener!=null && parent.isEditable()) {
            listBoxItemChangedListener.onItemChanged(this, value);
        }
    }
}
